/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package suac.familia;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import suac.dependente.Dependente;
import suac.pessoaref.PessoaRef;
import suac.util.GenericDAO;
import suac.util.HibernateUtil;

/**
 *
 * @author marcos
 */
public class FamiliaDAO extends GenericDAO<Familia> {

    public FamiliaDAO() {
        super(Familia.class);
    }

    public List<Familia> procuraNomeFamilia(String nome) {
        List<Familia> familias = null;

        try {
            this.setSessao(HibernateUtil.getSessionFactory().openSession());
            this.setTransacao(getSessao().beginTransaction());

            familias = (List<Familia>) getSessao().createCriteria(Familia.class).
                    add(Restrictions.ilike("nome_familia", nome, MatchMode.ANYWHERE)).
                    addOrder(Order.asc("nome_familia")).list();

        } catch (HibernateException e) {
            System.out.println("Erro ao procurar por Nome da Familia: " + e.getMessage());
        } finally {
            this.getSessao().close();
        }
        return familias;
    }

    public Familia pesquisarCodigo(short codigo) {
        Familia familia = null;
        try {
            this.setSessao(HibernateUtil.getSessionFactory().openSession());
            this.setTransacao(getSessao().beginTransaction());

            familia = (Familia) getSessao().createCriteria(Familia.class).
                    add(Restrictions.eq("idFamilia", codigo)).uniqueResult();

        } catch (HibernateException e) {
            System.out.println("Erro ao procurar por código: " + e.getMessage());
        } finally {
            this.getSessao().close();
        }
        return familia;
    }
    
        public List<Familia> pesquisarDescricaoLike(String nome) {
        List<Familia> familia = null;
        try {
            this.setSessao(HibernateUtil.getSessionFactory().openSession());
            this.setTransacao(getSessao().beginTransaction());

            familia = (List<Familia>) getSessao().createCriteria(Familia.class).
                    add(Restrictions.ilike("nome_familia", nome, MatchMode.ANYWHERE)).list();

        } catch (HibernateException e) {
            System.out.println("Erro ao procurar por Família: " + e.getMessage());
        } finally {
            this.getSessao().close();
        }
        return familia;
    }
}
